<template>
  <div id="placeorder">
    <div class="bg"></div>
    <!-- 头 -->
    <div class="haerder">
      <div class="backBtn" @click="goback()">
        <img src="https://mall.leyifan.com/static/h5/new_images/goods_back.png" alt="" />
      </div>
      <div class="title">
        <span>提交订单</span>
      </div>
    </div>
    <!-- 主体 -->
    <div class="content">
      <div class="top">
        <div class="orderGoods">
          <div class="total">
            <div class="image">
              <img
                src="https://image1.leyifan.cn/reborn-mall-cdn/crmebimage/public/config/2024/12/12/d533b2efe4b34dc4bcd77f8e63dce59ebaq9l2l0n4.png"
                alt=""
              />
            </div>
            <div class="sum">
              <span>共{{ totalQuantity }}件商品</span>
            </div>
          </div>
          <!-- <img src="https://image1.leyifan.cn/mercari-cdn/item/detail/orig/photos/m33266874929_1.jpg?1711926568"/> -->

          <div v-if="storeId == null">
            <div
              class="goodWrapper"
              v-for="item in orderItems"
              :key="item.id"
              style="margin-bottom: 20rem"
            >
              <div class="item" style="margin-bottom: 20rem">
                <div class="picture">
                  <img :src="item.imgurl" />
                </div>
                <div class="info1">
                  <div class="name">
                    <span class="linle">{{ item.name }}</span>
                    <span class="num">x{{ item.num }}</span>
                  </div>
                  <div class="attr">
                    <span class="left">属性:</span>
                    <span class="right">默认</span>
                  </div>
                  <div class="service">
                    <span class="left">可选服务:</span>
                    <span class="right">无</span>
                  </div>
                  <div class="price">
                    <span class="price-oppo">{{ item.price }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <div v-else class="goodWrapper" style="margin-bottom: 20rem">
            <div class="item" style="margin-bottom: 20rem">
              <div class="picture">
                <img :src="images" />
              </div>
              <div class="info1">
                <div class="name">
                  <span class="linle">{{ myitem?.storeName }}</span>
                  <span class="num">x{{ 1 }}</span>
                </div>
                <div class="attr">
                  <span class="left">属性:</span>
                  <span class="right">默认</span>
                </div>
                <div class="service">
                  <span class="left">可选服务:</span>
                  <span class="right">无</span>
                </div>
                <div class="price">
                  <span class="price-oppo">{{ myitem?.price }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="info">
        <li class="item">
          <span class="left">汇率</span>
          <span class="right">1日元 ≈ {{ 0.0049 }} 人民币</span>
        </li>
        <li class="item">
          <span class="left">日本国内运费</span>
          <span class="right">免运费</span>
        </li>
        <li class="item">
          <span class="left">优惠券</span>
          <span class="right">点击选择优惠券</span>
        </li>
        <li class="item">
          <span class="left">积分抵扣</span>
          <span class="right">在线支付</span>
        </li>
      </div>
      <div class="card">
        <div class="title">支付方式</div>
        <div class="pay-list">
          <ul>
            <li class="item">
              <div class="left">
                <div class="img">
                  <img src="https://mall.leyifan.com/static/h5/new_images/pay_wechat.png" alt="" />
                </div>
                <div class="text">
                  <div class="title2">微信支付</div>
                  <div class="subtitle">
                    <div>微信快捷支付</div>
                  </div>
                </div>
              </div>
              <div class="right">
                <div class="inp">
                  <van-radio
                    name="wecht"
                    v-model="selectedPaymentMethod"
                    checked-color="#ff546d"
                  ></van-radio>
                </div>
              </div>
            </li>
            <li class="item">
              <div class="left">
                <div class="img">
                  <img src="https://mall.leyifan.com/static/h5/new_images/pay_balance.png" alt="" />
                </div>
                <div class="text">
                  <div class="title2">余额支付</div>
                  <div class="subtitle">
                    <div>余额为零</div>
                  </div>
                </div>
              </div>
              <div class="right">
                <div class="inp">
                  <van-radio
                    name="balance"
                    v-model="selectedPaymentMethod"
                    checked-color="#ff546d"
                  ></van-radio>
                </div>
              </div>
            </li>
            <li class="item">
              <div class="left">
                <div class="img">
                  <img src="https://mall.leyifan.com/static/h5/new_images/pay_paypal.png" alt="" />
                </div>
                <div class="text">
                  <div class="title2">Paypal+5%手续费</div>
                  <div class="subtitle">
                    <div>paypal快捷支付</div>
                  </div>
                </div>
              </div>
              <div class="right">
                <div class="inp">
                  <van-radio
                    name="paypal"
                    v-model="selectedPaymentMethod"
                    checked-color="#ff546d"
                  ></van-radio>
                </div>
              </div>
            </li>
            <li class="item">
              <div class="left">
                <div class="img">
                  <img src="https://mall.leyifan.com/static/h5/new_images/pay_alipay.png" alt="" />
                </div>
                <div class="text">
                  <div class="title2">支付宝支付</div>
                  <div class="subtitle">
                    <div>支付宝快捷支付</div>
                  </div>
                </div>
              </div>
              <div class="right">
                <div class="inp">
                  <van-radio
                    name="alipay"
                    v-model="selectedPaymentMethod"
                    checked-color="#ff546d"
                  ></van-radio>
                </div>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div class="confirmCaed">
        <div class="inp">
          <div class="left">
            <input type="checkbox" v-model="agreed" />
          </div>
          <div class="text">
            <span>我已阅读并同意以下事项</span>
          </div>
        </div>
        <div class="list">
          <div class="item">
            <div class="list-item">
              <div class="ttt"></div>
              <div class="text">
                乐淘一番仅提供
                <span class="red">撮合交易平台</span>，其中
                <span class="red">代购、代拍及转运服务由株式会社乐一番提供</span
                >，由于海淘代购服务的特殊性决定无法保证卖家所售商品的真伪和质量，且不承担商品相关真伪和质量的赔偿责任。
              </div>
            </div>
          </div>
          <div class="item">
            <div class="list-item">
              <div class="ttt"></div>
              <div class="text">
                鉴于海淘代购的特殊性，在下单前请仔细阅读商家的商品详情及要求。平台内所有订单在完成支付后，均不接受来自买家的取消、退款或退货申请。请您先确认好购买意向再决定下单。您可以在网页端/小程序
                我的-使用指南查看
                <span class="bule">【购物风险提示】</span>
              </div>
            </div>
          </div>
          <div class="item">
            <div class="list-item">
              <div class="ttt"></div>
              <div class="text">
                由于国际物流的特殊性，针对商品的外包装出现挤压、变形、破损、污染及非商品本体的损坏、缺失等情况提出的退换货申请及赔偿，乐淘一番将无法受理。
              </div>
            </div>
          </div>
          <div class="item">
            <div class="list-item">
              <div class="ttt"></div>
              <div class="text">
                各物流方式的价格、时效及禁运品限制等存在差异。请确保您购买的商品符合当地法律法规，并确认其不厘于禁运商品。对于违禁类商品，乐淘一番将取消订单，且不支持退货退款。
                <span class="red"
                  >您在乐一番转运后台【申请发货】页选择邮毒方式前，请务必仔细阅读各条线路的详情介绍，因线路限制导致无法寄送所造成的损失，本平台概不负责</span
                >
              </div>
            </div>
          </div>
          <div class="item">
            <div class="list-item">
              <div class="ttt"></div>
              <div class="text">
                由于商品均为跨境采购及发货，商品货权为跨境企业或个人卖家，仅可提供采购当地的票据或凭证，无法开具您所在国家或地区的发票或票据。入库包事仅提供国际转运服务，不支持自提及日本境内转寄服务。*注:使用乐一番转运服务时需要完成身份认证，请您在乐一番后台上传正确身份信息。
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="buttom">
        <div class="create-addr" @click="createAddress()">
          <div class="text">
            <span>创建收货地址</span>
          </div>
          <div class="imge">
            <img src="https://mall.leyifan.com/static/h5/new_images/icon_right.png" alt="" />
          </div>
        </div>
      </div>
    </div>
    <!-- 底部 -->
    <div class="footer">
      <div class="box">
        <div class="box-left">
          <div class="total">
            <div class="name">
              <span>合计:</span>
            </div>
          </div>
          <div class="pric_box">
            <div class="text-top">
              <span>{{ totalPrice }}</span>
              <span>日元</span>
            </div>
            <div class="text-buttom">
              <span>约{{ Math.round(totalPrice * 0.0049) }}人民币</span>
            </div>
          </div>
        </div>
        <div :class="['textBtn', { active: agreed }]" :disabled="!agreed" @click="submitOrder">
          立即结算
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import axios from '@/api/axios'
const selectedPaymentMethod = ref('wechat')
const agreed = ref(false)
const router = useRouter()
const route = useRoute()
let storeId = route.query.id as string
const myitem = ref(null)
const images = ref([])
const orderItems = ref([])

const totalQuantity = computed(() => {
  return orderItems.value.reduce((total, item) => total + item.num, 0)
})
const totalPrice = computed(() => {
  return orderItems.value.reduce((total, item) => total + item.price, 0)
})

const isOrderSubmitted = ref(localStorage.getItem('isOrderSubmitted') === 'true')
const storedOrderItems = localStorage.getItem('orderItems')
console.log(storeId)

if (!storeId) {
  if (storedOrderItems) {
    try {
      orderItems.value = JSON.parse(storedOrderItems)
      console.log(orderItems.value)
    } catch (error) {
      console.error('解析 orderItems 失败', error)
    }
  }
} else {
  axios
    .get(
      `https://api.mall.leyifan.com/api/front/products/mercari/detailV3?type=normal&url=https%3A%2F%2Fjp.mercari.com%2Fitem%2F${storeId}&id=${storeId}&itemType=mercari`
    )
    .then((res) => {
      // console.log('q111111111', res.data.data.productInfo)
      myitem.value = res.data.data.productInfo
      console.log('myitem', myitem)

      images.value = res.data.data?.productInfo?.image
      console.log(images.value)

      images.value = images.value.replace(
        'https://static.mercdn.net',
        'https://image1.leyifan.cn/mercari-cdn'
      )

      // console.log('images', images.value)
    })
    .catch((error) => {
      console.error('请求失败', error)
    })
}

const goback = () => {
  
 
  showConfirmDialog({
  title: '订单未提交',
  message:
    '您还没有完成订单，是否要返回？',
})
  .then(() => {
    isOrderSubmitted.value = false;
    let orderItems = localStorage.getItem('orderItems');
    
  localStorage.setItem('isOrderSubmitted', 'false');
  router.go(-1);
    // on confirm
  })
  .catch(() => {
    // on cancel
  });
};
  isOrderSubmitted.value = false
  localStorage.setItem('isOrderSubmitted', 'false')
  router.go(-1)
}

const submitOrder = () => {
  if (agreed.value && selectedPaymentMethod.value) {
    isOrderSubmitted.value = true
    localStorage.setItem('isOrderSubmitted', 'true')
    // 提交订单的逻辑
    console.log('订单已提交')
    router.push('/myorder')
  }
}

const createAddress = () => {
  router.push('/address')
}
</script>


<style scoped lang="less">
#placeorder {
  position: relative;
  background-color: #f5f5f5;
  .bg {
    width: 100vw;
    height: 126rem;
    position: fixed;
    top: 0;
    left: 0;
    background-image: url(https://mall.leyifan.com/static/h5/new_images/shopping_bg.png);
    background-size: cover;
    z-index: -1;
  }
  .haerder {
    width: 375rem;
    height: 42rem;
    min-height: 42rem;
    top: 0;
    display: flex;
    background-color: rgba(0, 0, 0, 0);
    background-image: url(https://mall.leyifan.com/static/h5/new_images/shopping_bg.png);
    background-size: cover;
    z-index: 20;
    position: fixed;
    border-bottom: unset;
    .backBtn {
      height: 42rem;
      z-index: 100;
      position: absolute;
      left: 0;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      padding-left: 14rem;
      img {
        width: 16rem;
        height: 16rem;
      }
    }
    .title {
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      width: 100%;
      font-size: 17rem;
      color: #333;
      font-weight: 700;
      height: 100%;
      width: 100vw;
      padding: 14rem 0;
      margin: 0 10rem;
    }
  }
  .content {
    padding: 0 14rem;
    width: 100%;
    height: 1300rem;
    margin-top: 62rem;
    .top {
      margin-top: 10rem;
      margin-bottom: 10rem;
      .orderGoods {
        background-color: #fff;
        border-radius: 8rem;
        padding: 14rem 10rem;
        .total {
          height: 22rem;
          margin-bottom: 14rem;
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-bottom: 14rem;
          .image {
            img {
              width: 80rem;
              height: 22rem;
              display: inline-block;
              overflow: hidden;
              position: relative;
            }
          }
          .sum {
            color: #999;
            font-size: 12rem;
          }
        }
        .goodWrapper {
          width: 327rem;
          height: 85rem;
          .item {
            width: 100%;
            height: 100%;
            display: flex;
            height: auto;
            border: none;
            .picture {
              width: 80rem;
              height: 80rem;
              min-width: 66rem;
              min-height: 66rem;
              border-radius: 6rem;
              background-color: #f5f5f5;
              margin-right: 10rem;
              border: 1rem solid #f5f5f5;
            }
            .info1 {
              overflow: hidden;
              flex: 1;
              .name {
                font-size: 14rem;
                line-height: normal;
                color: #333;
                margin-bottom: 10rem;
                display: flex;
                justify-content: space-between;
                overflow: hidden;
                align-items: center;
                white-space: nowrap; /* 添加这一行 */
                width: 210rem;
                .linle {
                  overflow: hidden;
                  text-overflow: ellipsis;
                  white-space: nowrap; /* 确保继承 */
                }
              }
              .attr {
                font-size: 12rem;
                color: #999;
                line-height: 12rem;
                margin-bottom: 10rem;
                display: flex;
                align-items: center;
                width: 100%;
                .left {
                  min-width: 36rem;
                }
                .right {
                  flex: 1;
                  word-break: break-all;
                }
              }
              .service {
                margin-bottom: 10rem;
                font-size: 12rem;
                color: #999;
                line-height: 12rem;
                .left {
                  min-width: 36rem;
                }
                .right {
                  flex: 1;
                  word-break: break-all;
                }
              }
              .price {
                margin-bottom: 10rem;
                font-size: 12rem;
                line-height: 12rem;
                .left {
                  min-width: 36rem;
                }
                .right {
                  flex: 1;
                  word-break: break-all;
                }
              }
            }
          }
        }
      }
    }
    .info {
      background-color: #fff;
      border-radius: 8rem;
      padding: 14rem 10rem;
      li {
        list-style: none;
        height: 19rem;
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 10rem;
        .left {
          font-size: 12rem;
          line-height: 12rem;
          color: #999;
          display: flex;
        }
        .right {
          font-size: 12rem;
          line-height: 12rem;
          color: #333;
          display: inline-flex;
          align-items: center;
        }
      }
    }
    .card {
      background-color: #fff;
      border-radius: 8rem;
      padding: 0 10rem;
      margin-top: 10rem;
      .title {
        border-bottom: 1rem solid #f5f5f5;
        font-size: 12rem;
        color: #333;
        line-height: 12rem;
        padding: 14rem 0;
      }
      .pay-list {
        padding: 0 10rem;
        ul {
          .item {
            list-style: none;
            display: flex;
            padding: 14rem 0;
            border-bottom: 1rem solid #f5f5f5;
            justify-content: space-between;
            align-items: center;
            transition: background-color 0.2s ease-in-out;
            .left {
              display: flex;
              align-items: center;
              .img {
                width: 30rem;
                height: 30rem;
                margin-right: 10rem;
                img {
                  width: 100%;
                  height: 100%;
                }
              }
              .text {
                .title2 {
                  font-size: 13rem;
                  color: #333;
                  line-height: 13rem;
                  margin-bottom: 6rem;
                }
                .subtitle {
                  font-size: 11rem;
                  color: #999;
                  line-height: 11rem;
                }
              }
            }
          }
        }
      }
    }
    .confirmCaed {
      background-color: #fff;
      border-radius: 8rem;
      padding: 0 10rem;
      margin-top: 10rem;
      .inp {
        padding: 14rem 0 16rem;
        display: flex;
        align-items: center;
        font-size: 13rem;
        line-height: 13rem;
        border-bottom: 1rem solid #f5f5f5;
        .left {
          -webkit-tap-highlight-color: transparent;
          display: inline-block;
          cursor: pointer;
        }
        .text {
          margin-left: 6rem;
        }
      }
      .list {
        padding: 14rem 0;
        border-bottom: 1rem solid #f5f5f5;
        .item {
          .list-item {
            display: flex;
            .ttt::before {
              content: '';
              display: inline-block;
              width: 4rem;
              height: 4rem;
              border-radius: 50%;
              background-color: #999;
              margin-right: 10rem;
              margin-bottom: 7rem;
            }
            .text {
              color: #999;
              font-size: 12rem;
              line-height: 18rem;
              flex: 1;
              .red {
                color: red;
              }
              .bule {
                color: #95eaff;
              }
            }
          }
        }
      }
    }
    .buttom {
      background-color: #fff;
      border-radius: 8rem;
      padding: 0 10rem;
      margin-top: 10rem;
      .create-addr {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 14rem 10rem;
        font-size: 12rem;
        .imge {
          img {
            width: 10rem;
            height: 10rem;
          }
        }
      }
    }
  }
  .footer {
    width: 100%;
    background-color: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
    .box {
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 14rem;
      height: 66rem;
      .box-left {
        display: flex;
        .total {
          width: 44rem;
          height: 34rem;
          text-align: center;
          .name {
            font-size: 14rem;
            line-height: 14rem;
            color: #333;
            margin-right: 6rem;
            span {
              font-size: 14rem;
              line-height: 14rem;
              color: #333;
              margin-right: 6rem;
            }
          }
        }
        .pric_box {
          .text-top {
            color: #ff3d58;
            font-size: 16rem;
            line-height: 15rem;
            font-weight: 700;
            margin-right: 2rem;
          }
          .text-buttom {
            margin-top: 3rem;
            font-size: 12rem;
            color: #333;
            line-height: 12rem;
          }
        }
      }
      .textBtn {
        background-color: #ccc;
        color: #fff;
        font-size: 14rem;
        height: 38rem;
        line-height: 38rem;
        text-align: center;
        border-radius: 19rem;
        padding: 0 20rem;
        font-weight: 700;
        &.active {
          background-color: #ff546d; // 激活状态下的背景颜色
          cursor: pointer; // 激活状态下的鼠标指针样式
        }

        &:disabled {
          opacity: 0.6; // 禁用状态下的透明度
        }
      }
    }
  }
}
</style>